package com.twu.biblioteca.domain;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: sagar
 * Date: 19/9/13
 * Time: 10:52 AM
 * To change this template use File | Settings | File Templates.
 */
public  class Book implements Displayable {

    private int id;
    private String name;
    private String author;
    private String publisherName;
    private int year;
    private int totalQuantity;
    private int totalBorrowed;
    private ArrayList<User> userBorrowed;
    public static final Book NON_EXISTENT_BOOK = new Book();
    private Book(){

    }
    public Book(int id, String name, String author, String publisherName, int year, int totalQuantity, int totalBorrowed) {
        userBorrowed=new ArrayList<User>();
        this.id = id;
        this.name = name;
        this.author = author;
        this.publisherName = publisherName;
        this.year = year;
        this.totalQuantity = totalQuantity;
        this.totalBorrowed = totalBorrowed;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }


    public String getAuthor() {
        return author;
    }


    public String getPublisherName() {
        return publisherName;
    }
    public int getYear() {
        return year;
    }

    private boolean isAvailable(){
       return (totalQuantity>totalBorrowed);
    }

    public boolean issueCopy(User user){
        if(isAvailable())
        {  totalBorrowed++;
            userBorrowed.add(user);
            return true;
        }
       return false;
    }

    @Override
    public boolean equals(Object object){
           if(object instanceof  Book){
               Book testBook=(Book) object;
               if(testBook.getName().equals(name)&&testBook.getId()==id&&testBook.getAuthor().equals(author)&&testBook.getYear()==year&&testBook.getPublisherName().equals(publisherName))
                   return true;
           }
        return false;
    }

    @Override
    public String getDisplayText() {
        String bookString="Book Name: "+name+"\nBook Author: "+author+"\nBook Publisher: "+publisherName+"\nYear of Publishing: "+year+"\n";
        return bookString;
    }
}